"use strice";

const Controller = require('../common/shop')

class GoodsController extends Controller {

  tableName = 'goods'

  async goodsAdd() {
    let siteId = await this.getSiteId()
    let site_id = siteId[0].site_id

    console.log(this.ctx.request.body);

    this.tableName = 'shop'
    let shopResult = await this.consult({site_id})
    console.log('shopResult' ,shopResult);

    let {
      goods_content, goods_image, goods_name, goods_state, goods_brand, category_id, price, market_price, cost_price, introduction, is_consume_discount, is_free_shipping, keywords, max_buy, min_buy, goods_stock, unit, goods_spec_format, goodsSku
    } = this.ctx.request.body

    const [brand_id, brand_name] = goods_brand.split(',')

    category_id = category_id[category_id.length-1]

    let goodsResult

    this.tableName = 'goods'

    if (!goods_spec_format) {

      goodsResult = await this.add({ site_id, site_name: shopResult[0].shop_name, goods_content, goods_image, goods_name, goods_state, category_id, brand_id, brand_name, price, market_price, cost_price, introduction, is_consume_discount, is_free_shipping, keywords, max_buy, min_buy, goods_stock, unit, goods_spec_format })
    
    } else {
      price = goodsSku[0].price
      market_price = goodsSku[0].market_price
      cost_price = goodsSku[0].cost_price
      goods_stock = goodsSku.reduce((prev, next) => prev + next.stock, 0)
      console.log('aaaaaaaa', price, market_price, cost_price, goods_stock);
      goods_spec_format = JSON.stringify(goods_spec_format)
      goodsResult = await this.add({ site_id, goods_content, goods_image, goods_name, goods_state, category_id, brand_id, brand_name, create_time: Date.now(), modify_time: Date.now(), price, market_price, cost_price, introduction, is_consume_discount, is_free_shipping, keywords, max_buy, min_buy, goods_stock, unit, goods_spec_format })
      // insertId
      this.tableName = 'goods_sku'

      let skuPm = []

      goodsSku.forEach(item => {
        skuPm.push[this.add({goods_id: goodsResult.insertId, site_id, site_name: shopResult[0].shop_name, goods_name, sku_name: goods_name, create_time: Date.now(), modify_time: Date.now(), discount_price: item.price, goods_content, goods_state, introduction,  is_consume_discount, is_free_shipping, keywords, max_buy, min_buy, goods_spec_format, ...item})]
      });

      let skuResult = await Promise.all(skuPm)
      console.log('skuResult', skuResult);
    }

    console.log('goodsResult', goodsResult);

    this.resData()
  }

  async goodsDel() {
    console.log(this.ctx.request.body);
    this.resData(2, '成功的信息', { fasdfas: 'dasfasdfsa' })
  }

  async goodsList() {
    let siteId = await this.getSiteId()
    let site_id = siteId[0].site_id

    const {cur_page=1, each_page=10} = this.ctx.request.query
    let goodsData = await this.consultPage({site_id}, null, {curPage: cur_page, eachPage: each_page})

    this.tableName = 'goods_sku'
    let skuPm = []
    goodsData.list.forEach((goodsItem) => {
      skuPm.push(this.consult({goods_id: goodsItem.goods_id}))
    });
    let skuList = await Promise.all(skuPm)

    goodsData.list.forEach((goodsItem, index) => {
      goodsItem.skuList = skuList[index]
    })

    // console.log('goodsList', result);
    this.resData(0, '商品列表获取成功', goodsData)
  }

}

module.exports = GoodsController